package com.chen.spring.cloud.service.order.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chen.entity.Order;
import com.chen.entity.OrderGoods;
import com.chen.spring.cloud.service.order.service.IOrderGoodsService;
import com.chen.spring.cloud.service.order.service.IZsOrderService;
import com.chen.vo.OrderListVo;
import com.chen.vo.OrderSubVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/Order")
public class OrderController {

    @Autowired
    private IZsOrderService zsOrderService;

    @Autowired
    private IOrderGoodsService orderGoodsService;

    @RequestMapping("/getListByLimit")
    public Map<String,Object> getListByLimit(Integer uId, String is_appointment, String limit, String is_refund, String status){
        Map<String,Object> result = new HashMap<>();
        Page<Order> page = new Page<>();
        if (!StringUtils.isEmpty(limit)){
            String[] pageInfo = limit.split(",");
            page.setCurrent(Integer.parseInt(pageInfo[0]));
            page.setSize(Integer.parseInt(pageInfo[1]));
        }else {
            page.setCurrent(1);
            page.setSize(15);
        }
        QueryWrapper<Order> wrapper = new QueryWrapper<Order>().eq("u_id",uId).eq("is_delete",0);
        if (!StringUtils.isEmpty(is_appointment)){
            wrapper.eq("is_appointment",is_appointment);
        }
        if (!StringUtils.isEmpty(is_refund)){
            wrapper.eq("is_refund",is_refund);
        }
        if (!StringUtils.isEmpty(is_refund)){
            wrapper.eq("status",status);
        }
        wrapper.orderByDesc("o_id");

        Page<Order> data = (Page<Order>) zsOrderService.page(page,wrapper);
        List<OrderListVo> listVos = new ArrayList<>();
        for (Order zsOrder:data.getRecords()) {
            OrderListVo orderListVo = new OrderListVo();
            BeanUtil.copyProperties(zsOrder,orderListVo);
            orderListVo.setUnit_amount(zsOrder.getTotal_amount() / zsOrder.getQuantity());
            orderListVo.setOri_amount(zsOrder.getTotal_amount() + zsOrder.getDis_amount());
            listVos.add(orderListVo);
        }
        result.put("totalcount",data.getTotal());
        result.put("list",listVos);
        return result;
    }

    @RequestMapping("/del")
    public boolean del(String order_sn,int u_id){
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("order_sn",order_sn);
        wrapper.eq("u_id",u_id);
        Order order = zsOrderService.getOne(wrapper);
        if (order != null){
            order.setIs_delete(true);
            return zsOrderService.updateById(order);
        }
        return false;
    }


    @RequestMapping("/details")
    public Map<String,Object> details(String order_sn,int u_id){
        Map<String,Object> result = new HashMap<>();
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("order_sn",order_sn);
        wrapper.eq("u_id",u_id);
        Order order = zsOrderService.getOne(wrapper);

        if (order.getSpan_time() != null){
            switch (order.getSpan_time()){
                case 0:
                    order.setSpan_start_time("08:30-10:30");
                    break;
                case 1:
                    order.setSpan_start_time("10:30-12:30");
                    break;
                case 2:
                    order.setSpan_start_time("12:30-14:30");
                    break;
                case 3:
                    order.setSpan_start_time("14:30-16:30");
                    break;
                case 4:
                    order.setSpan_start_time("16:30-17:00");
                    break;
            }
        }

        List<OrderGoods> orderGoodsList = orderGoodsService.list(new QueryWrapper<OrderGoods>().eq("o_id",order.getO_id()));
        OrderSubVo orderSubVo = orderGoodsService.getSubInfo(u_id,order.getO_id());

        result.put("order",order);
        result.put("goods",orderGoodsList);
        result.put("visitors",orderSubVo);

        return result;
    }
}
